import assert = require('assert');
import {exp} from "../expressExt/exp";

@exp.noValid
class ReqTest {
    id = 1;
    val = "2";

    @exp.int(r => r.val > 0, "aaa必须大于0")
    aaa = 0;

    @exp.string(/^123$/, "bbb错误")
    bbb = "b";
}

class NotVaild {

}


class ReqTest2 {
    id = 1;
    val = "2";

    @exp.int(r => r.val > 0, "aaa必须大于0")
    aaa = 0;


    @exp.string()
    bbb = "b";
}

class ReqTest3 {
    id = 1;
    val = "2";


    bbb = new ReqTest2();
}


class ReqTest4 {
    id = 1;
    val = "2";


    @exp.object<ReqTest2>(r => r.val.bbb.length > 0, "bbb.bbb 不能为空")
    bbb = new ReqTest2();
}

class ArrayTest {

    @exp.array(r => r.val.eachToInt())
    bbb = [];

    @exp.array(r => r.val.eachToString())
    cccc = [];

    @exp.array(r => r.val.eachToObj(ReqTest2))
    ddd: ReqTest2[] = [];
}


describe('ExpressExt Test', function () {

    it('ExpressExt route array入参验证', function () {
        let notRes = exp.checkObj({
            bbb: [],
            cccc: [],
            ddd: [],
        }, new ArrayTest());
        assert.equal(notRes.ok, true);

        notRes = exp.checkObj({
            bbb: null,
            cccc: [],
            ddd: [],
        }, new ArrayTest());
        assert.equal(notRes.ok, true);
        assert.equal(JSON.stringify(notRes.val), `{\"bbb\":[],\"cccc\":[],\"ddd\":[]}`);

        notRes = exp.checkObj({
            bbb: ["22", "11.1", 33],
            cccc: [1, 2, "3"],
            ddd: [],
        }, new ArrayTest());
        assert.equal(notRes.ok, true);
        assert.equal(JSON.stringify(notRes.val), `{"bbb":[22,11,33],"cccc":["1","2","3"],"ddd":[]}`);

        notRes = exp.checkObj({
            bbb: ["22", "11.1", 33],
            cccc: [1, 2, "3"],
            ddd: [new ReqTest2()],
        }, new ArrayTest());
        assert.equal(notRes.ok, false);
        assert.equal(notRes.msg, "ddd 参数无效");

        notRes = exp.checkObj({
            bbb: ["22", "11.1", 33],
            cccc: [1, 2, "3"],
            ddd: [1],
        }, new ArrayTest());
        assert.equal(notRes.ok, false);
        assert.equal(notRes.msg, "ddd 参数无效");

        let req = new ReqTest2();
        req.aaa = 2;
        notRes = exp.checkObj({
            bbb: ["22", "11.1", 33],
            cccc: [1, 2, "3"],
            ddd: [req, req],
        }, new ArrayTest());
        assert.equal(notRes.ok, true);
        assert.equal(JSON.stringify(notRes.val), `{"bbb":[22,11,33],"cccc":["1","2","3"],"ddd":[{"id":1,"val":"2","aaa":2,"bbb":"b"},{"id":1,"val":"2","aaa":2,"bbb":"b"}]}`);
    });

    it('ExpressExt route 入参验证', function () {

        let notRes = exp.checkObj({}, new NotVaild());
        assert.equal(notRes.ok, true);


        let res = exp.checkObj({
            aaa: 2,
            bbb: "123",
        }, new ReqTest());
        assert.equal(res.ok, true);
        assert.equal(res.val.id, 1);
        assert.equal(res.val.val, "2");

        res = exp.checkObj({
            aaa: 2,
            bbb: "aaa",
        }, new ReqTest());
        assert.equal(res.ok, false);
        assert.equal(res.msg, "bbb错误");


        res = exp.checkObj({
            aaa: 0,
            bbb: "123",
        }, new ReqTest());
        assert.equal(res.ok, false);
        assert.equal(res.msg, "aaa必须大于0");

        res = exp.checkObj({
            aaa: 1,
        }, new ReqTest2());
        assert.equal(res.ok, false);
        assert.equal(res.msg, "bbb " + exp.errMsg_);

        let re2 = exp.checkObj({
            bbb: {
                aaa: 10,
                bbb: "123",
            }
        }, new ReqTest3());
        assert.equal(re2.ok, true);

        re2 = exp.checkObj({
            bbb: {
                aaa: 0,
                bbb: "123",
            }
        }, new ReqTest3());
        assert.equal(re2.ok, false);
        assert.equal(re2.msg, "aaa必须大于0");

        re2 = exp.checkObj({
            bbb: {}
        }, new ReqTest3());
        assert.equal(re2.ok, false);
        assert.equal(re2.msg, "aaa必须大于0");


        re2 = exp.checkObj({
            bbb: {
                aaa: 1,
                bbb: "",
            }
        }, new ReqTest4());
        assert.equal(re2.ok, false);
        assert.equal(re2.msg, "bbb.bbb 不能为空");

        re2 = exp.checkObj({
            bbb: {
                aaa: 1,
                bbb: "1",
            }
        }, new ReqTest4());
        assert.equal(re2.ok, true);

    });
});